package jp.co.cos.ittrick.channel.common.filter;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class FilesFilter implements Filter {

	Logger logger = Logger.getLogger(this.getClass().getName());


	public void init(FilterConfig config) throws ServletException {

	}

	public void destroy() {

	}

	/**
	 * doFilter
	 * SitemapsAction を呼び出す
	 */
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
	throws IOException, ServletException {

		String changedURI = "/FilesAction.do";
		String reqURI = (((HttpServletRequest) req).getRequestURI()).toString();
		String filePath;


		// フォーワード
		if(changedURI != null && !changedURI.equals("")) {

			filePath = reqURI.replaceAll("/files/", "/");
			changedURI = changedURI + "?filePath=" + filePath;

			try {
				RequestDispatcher rd = req.getRequestDispatcher(changedURI);
				rd.forward(req, res);
				return;
			} catch(NullPointerException e) {}
		}

	}
}
